package com.dxw.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class CollectionIterator {
    public static void main(String[] args) {
        Collection col = new ArrayList();
        col.add(new Book("三国演义","罗贯中",10.1));
        col.add(new Book("三国","罗贯中",10.1));
        col.add(new Book("演义","罗贯中",10.1));
        //遍历col集合；
        //先得到col对应的迭代器，再使用while循环遍历
        Iterator it = col.iterator();
        while (it.hasNext()) {
            Object obj =  it.next();
            System.out.println("obj = " + obj);
        }
        //如果希望再次遍历，需要重置迭代器；因为next已经取到最后一位了，再取会报错；
//        it=col.iterator();
        System.out.println("=========");
        //增强for，在colletion集合
        //使用增强for，底层仍然是迭代器；
        //增强for，实际就是简化版的迭代器；
        for (Object obj : col) {
            System.out.println("obj = " + obj);
        }
        //增强for 也可以直接在数组中使用；
        int[] nums = {1,8,10,12};
        for (int num : nums) {
            System.out.println("num = " + num);
        }

    }
}
class Book{
    String name;
    String author;
    double price;
    public Book(String name, String author, double price) {
        this.name = name;
        this.author = author;
        this.price = price;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                '}';
    }
}